package com.sltx.service.impl;

import com.sltx.dao.aaa.ImageMapper;
import com.sltx.service.ImageService;
import com.sltx.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ImageServiceImpl implements ImageService {

    @Autowired
    ImageMapper imageMapper;

    @Override
    public int getlyjjNum(PageData pd){

        int i = imageMapper.getlyjjNum(pd);

        return i;
    }

    @Override
    public List<Object> getlyjj(PageData pd){
        List<Map<String, Object>> getlyjj = imageMapper.getlyjj(pd);
        List<Object> result = new ArrayList<>();
        for (Map m:getlyjj){
            if (m.get("pdf_url") != null) {
                String pdf_url = m.get("pdf_url").toString();
                if (pdf_url.lastIndexOf(".") != -1) {
                    String suffix = pdf_url.substring(pdf_url.lastIndexOf("."));
                    m.put("suffix", suffix);
                }
            }
            result.add(m);
        }
        return result;
    }

    @Override
    public int insertZnbg(PageData pd){
        int i = imageMapper.insertZnbg(pd);
        return i;
    }

    @Override
    public int insertFavorite(PageData pd){
        int i = imageMapper.insertFavorite(pd);
        return i;
    }


    @Override
    public int deleteFavorite(PageData pd){
        int i = imageMapper.deleteFavorite(pd);
        return i;
    }


    @Override
    public int updateZnbg(PageData pd){
        int i = imageMapper.updateZnbg(pd);
        return i;
    }

    @Override
    public int getListNum(PageData pd){
        int i = imageMapper.getListNum(pd);
        return i;
    }

    @Override
    public List<Object> getList(PageData pd){
        List<Object> result = new ArrayList<>();
        List<Map<String,Object>> list = imageMapper.getList(pd);

        for (Map m:list){
            if (m.get("file_url") != null) {
                String file_url = m.get("file_url").toString();
                if (file_url.lastIndexOf(".") != -1) {
                    String suffix = file_url.substring(file_url.lastIndexOf("."));
                    m.put("suffix", suffix);
                }
            }
            if (m.get("favorite") == null){
                m.put("favorite","0");
            }
            result.add(m);
        }
        return result;
    }

    @Override
    public int getDeptListNum(PageData pd){
        int i = imageMapper.getDeptListNum(pd);
        return i;
    }

    @Override
    public List<Object> getDeptList(PageData pd){
        List<Object> result = new ArrayList<>();
        List<Map<String,Object>> deptList = imageMapper.getDeptList(pd);

        for (Map m:deptList){
            if (m.get("file_url") != null) {
                String file_url = m.get("file_url").toString();
                String suffix = file_url.substring(file_url.lastIndexOf("."));
                m.put("suffix", suffix);
            }
//            if (m.get("favorite") == null){
//                m.put("favorite","0");
//            }
            result.add(m);
        }
        return result;
    }

    @Override
    public int getFavoriteNum(PageData pd){
        int i = imageMapper.getFavoriteNum(pd);
        return i;
    }

    @Override
    public List<Object> getFavorite(PageData pd){
        List<Object> result = new ArrayList<>();
        List<Map<String,Object>> favorite = imageMapper.getFavorite(pd);
        for (Map m: favorite){
            if (m.get("file_url") != null) {
                String file_url = m.get("file_url").toString();
                String suffix = file_url.substring(file_url.lastIndexOf("."));
                m.put("suffix", suffix);
            }
            if (m.get("favorite") == null){
                m.put("favorite","0");
            }
            result.add(m);
        }
        return result;
    }

    @Override
    public int getYorN(PageData pd){
        int i = imageMapper.getYorN(pd);
        return i;
    }
}
